<script setup lang="ts">
// 使用unplugin-vue-components默认会自动引入src/components目录下的sfc
// import CpNavBar from '@/components/CpNavBar.vue'
import { ref } from 'vue'
import { codeRules, mobileRules, passwordRules } from '@/utils/rules'
import { Toast, type FormInstance } from 'vant'
import { getCodeService, registerService } from '@/api/user'
import { useRouter } from 'vue-router'

const onClickRight = () => {
  router.replace('/login')
}
const showPassword = ref(false) // 密码是否显示

const registerData = ref({
  mobile: '',
  password: '',
  code: '',
})
const confirmPwd = ref('')
// 点击注册
const router = useRouter()
const register = async () => {
  if (registerData.value.password !== confirmPwd.value) {
    return Toast('两次输入的密码不一致')
  }
  try {
    await registerService(registerData.value)
    Toast.success('注册成功')
    // 注册成功后跳转到登录页
    router.push('/login')
  } catch (error) {
    console.log(error)
  }
}

// 发送验证码
const formRef = ref<FormInstance>()
const countdown = ref(0) // 倒计时时间
let timer: any = null // 存储定时器id
const getCode = async () => {
  try {
    await formRef.value?.validate('mobile')
    Toast.success('发送验证码成功')
  } catch (error) {
    return Toast.fail('请输入正确的手机号')
  }
  try {
    const { code } = await getCodeService(registerData.value.mobile, 'register')
    registerData.value.code = code
    countdown.value = 60 // 设置倒计时时间
    // 开启定时器
    timer = setInterval(() => {
      countdown.value--
      if (countdown.value === 0) clearInterval(timer)
    }, 1000)
  } catch (error) {}
}
</script>

<template>
  <cp-nav-bar title="注册" right-text="登录" @click-right="onClickRight" />
  <div class="login">
    <div class="login-head">
      <h3>注册</h3>
      <a href="javascript:;" @click="onClickRight">
        <span>密码登录</span>
        <van-icon name="arrow"></van-icon>
      </a>
    </div>
    <!-- == form 表单 == -->
    <van-form autocomplete="off" @submit="register" ref="formRef">
      <van-field
        placeholder="请输入手机号"
        type="tel"
        v-model="registerData.mobile"
        :rules="mobileRules"
        name="mobile"
      ></van-field>

      <van-field
        :placeholder="'请输入密码'"
        :type="showPassword ? 'text' : 'password'"
        v-model="registerData.password"
        :rules="passwordRules"
      >
        <template #button>
          <cp-icon
            :name="showPassword ? 'login-eye-on' : 'login-eye-off'"
            @click="showPassword = !showPassword"
          ></cp-icon>
        </template>
      </van-field>
      <van-field
        :placeholder="'请再次输入密码'"
        :type="showPassword ? 'text' : 'password'"
        v-model="confirmPwd"
        :rules="passwordRules"
      >
        <template #button>
          <cp-icon
            :name="showPassword ? 'login-eye-on' : 'login-eye-off'"
            @click="showPassword = !showPassword"
          ></cp-icon>
        </template>
      </van-field>
      <van-field
        :placeholder="'请输入验证码'"
        type="text"
        v-model="registerData.code"
        :rules="codeRules"
      >
        <template #button>
          <span class="get-code" @click="getCode" v-if="countdown === 0">获取验证码</span>
          <span class="sended-code" v-else>{{ countdown }}秒后重新获取</span>
        </template>
      </van-field>
      <div class="cp-cell">
        <van-button block round type="primary" native-type="submit">注 册</van-button>
      </div>
    </van-form>
  </div>
</template>

<style lang="scss" scoped>
.login {
  padding-top: 46px;
  &-head {
    padding: 30px 30px 50px;
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    line-height: 1;
    h3 {
      font-weight: bold;
      font-size: 24px;
    }
    a {
      font-size: 15px;
    }
  }
  .van-form {
    padding: 0 14px;
    .cp-cell {
      height: 52px;
      padding: 14px 16px;
      box-sizing: border-box;
      display: flex;
      align-items: center;
      .van-checkbox {
        a {
          color: var(--cp-primary);
          padding: 0 5px;
        }
      }
    }
    .get-code {
      color: var(--cp-primary);
    }
    .sended-code {
      color: var(--cp-price);
    }
  }
}
</style>
